package com.liang.blog.reception.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.liang.blog.client.user.UserFeignClient;
import com.liang.blog.common.exception.BlogException;
import com.liang.blog.common.result.Result;
import com.liang.blog.common.result.ResultCodeEnum;
import com.liang.blog.model.BlogMsgInfo;
import com.liang.blog.model.BlogSessionList;
import com.liang.blog.reception.mapper.BlogMsgInfoMapper;
import com.liang.blog.reception.mapper.BlogSessionListMapper;
import com.liang.blog.reception.service.BlogMsgInfoService;
import com.liang.blog.reception.service.BlogSessionListService;
import com.liang.blog.security.utils.UUIDUtil;
import com.liang.blog.vo.user.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author：YanWenLiang
 * @Date:2024-05-02-10:14
 */
@Service
public class BlogMsgInfoServiceImpl implements BlogMsgInfoService {

    @Autowired
    private BlogMsgInfoMapper blogMsgInfoMapper;

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private BlogSessionListService blogSessionListService;

    /**
     * 将websocket接收到的消息持久化
     * @param message
     * @param sessionList
     * @return
     */
    @Override
    public BlogMsgInfo createMsgInfoBymessage(String msgType, String message, BlogSessionList sessionList) {

        Result<UserInfoVo> userResult = userFeignClient.getUserById(sessionList.getBlogUserId());

        if (ResultCodeEnum.SUCCESS.getCode().intValue() != userResult.getCode().intValue()){
            throw new BlogException(ResultCodeEnum.SERVICE_ERROR);
        }

        UserInfoVo userInfoVo = userResult.getData();

        BlogMsgInfo msgInfo = new BlogMsgInfo();
        msgInfo.setMsgId(UUIDUtil.getUUID());
        msgInfo.setBlogMsgContent(message);
        msgInfo.setCreateTime(new Date());
        msgInfo.setFromBlogUserId(sessionList.getBlogUserId());
        msgInfo.setFromBlogUserName(userInfoVo.getNickName());
        msgInfo.setToBlogUserId(sessionList.getToBlogUserId());
        msgInfo.setToBlogUserName(sessionList.getBlogListName());
        msgInfo.setBlogUnReadFlag(false);
        msgInfo.setBlogMsgType(msgType);
        blogMsgInfoMapper.insert(msgInfo);
        return msgInfo;
    }

    /**
     * 根据sessionListId查询出当前会话窗口的历史聊天记录
     * 包括对方发给自己的和自己发给对方的消息
     * @param sessionListId
     * @return
     */
    @Override
    @Transactional
    public List<BlogMsgInfo> getMsgInfoList(String sessionListId) {
        BlogSessionList sessionList = blogSessionListService.getById(sessionListId);
        if(ObjectUtil.isEmpty(sessionList)){
            throw new BlogException("会话异常");
        }
        List<BlogMsgInfo> msgInfos = blogMsgInfoMapper.selectMsgInfoListByUserId(sessionList.getBlogUserId(), sessionList.getToBlogUserId());

        //查询消息成功之后，将消息更新为已读状态
        blogMsgInfoMapper.updateMsgInfoRead(sessionList.getBlogUserId(), sessionList.getToBlogUserId());
        //更新会话里的未读消息数
        blogSessionListService.emptySessionListUnReadCount(sessionList);

        return msgInfos;
    }
}
